package com.xyzwps.mvckotlin

import com.xyzwps.mvckotlin.config.WebProperties
import org.mybatis.spring.annotation.MapperScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.http.CacheControl
import org.springframework.web.reactive.config.ResourceHandlerRegistry
import org.springframework.web.reactive.config.ViewResolverRegistry
import org.springframework.web.reactive.config.WebFluxConfigurer
import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver
import java.util.concurrent.TimeUnit

@SpringBootApplication
@MapperScan(basePackages = ["com.xyzwps.mvckotlin.dao.mapper"])
class App(val webProps: WebProperties,
          val thymeleafReactiveViewResolver: ThymeleafReactiveViewResolver
) : WebFluxConfigurer {

    override fun configureViewResolvers(registry: ViewResolverRegistry) {
        registry.viewResolver(thymeleafReactiveViewResolver)
    }

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        val registration = registry
                .addResourceHandler("/**")
                .addResourceLocations("/public", "classpath:/static/")
        val maxAge = webProps.staticResourceMaxAge
        if (maxAge > 0) {
            registration.setCacheControl(CacheControl.maxAge(maxAge, TimeUnit.HOURS))
        }
    }
}

fun main(args: Array<String>) {
    runApplication<App>(*args)
}